
var BadApple = {
	is_running: false,
	data_index: 0,
	data_count: 67,
	is_loading: {},
	progress: $('#progress'),
	init: function() {
		var me = this,
			stage = $('#stage'),
			i;

		me.data_matrix = [];
		for(i = 0; i < me.frame_row; ++i) {
			me.data_matrix[i] = Array(me.frame_col).join(' ').split('');
		}

		me.canvas = stage.get(0);
		me.ctx = me.canvas.getContext('2d');
		me.canvas.width = me.frame_col * 7;
		me.canvas.height = me.frame_row * 16;

		me.ctx.fillStyle = 'rgba(0, 0, 0, .05)';
		me.ctx.fillRect(0, 0, me.canvas.width, me.canvas.height);
		me.ctx.fillStyle = '#7E7E7E';
		me.ctx.font = '12px monospace';
	},
	load_data: function(index, count, fn) {
		var me = this;
		if(!me.is_loading[index]) {
			// mark is loading
			me.is_loading[index] = true;
			$.get('data/data_' + index + '.txt', function(data) {
				// finish loading
				//console.log(data)
				me.is_loading[index] = null;
				me.data_index = index;
				data = data.split('\n');
				// update progress
				me.progress.html((index / me.data_count * 100).toFixed(2) + '%');

				if(me.data) {
					me.data = me.data.concat(data);
				}
				// first time
				else {
					me.data = data;
					var rc = data[0].split(' ');
					me.frame_row = parseInt(rc[0]);
					me.frame_col = parseInt(rc[1]);
					console.log("开始播放")
					me.init();
				}

				if(count > 1 && index < me.data_count) {
					me.load_data(index + 1, count - 1, fn);
				} else {
					fn && fn();
				}
			});
		}
	},
	draw_frame: function(data) {
		var me = this,
			i;
		for(i = 0; i < data.length; ++i) {
			me.data_matrix[data[i][0] - 1][data[i][1] - 1] = data[i][2];
		}
		me.ctx.clearRect(0, 0, me.canvas.width, me.canvas.height);
		for(i = 0; i < me.frame_row; ++i) {
			me.ctx.fillText(me.data_matrix[i].join(''), 4, i * 14 + 16);
		}
	},
	play: function() {
		var me = this,
			i = 0,
			j,
			l,
			r;
		me.is_running = true;
		me.interval || (me.interval = setInterval(function() {
			if(me.is_running && i < me.data.length) {
				l = me.data[++i];
				r = [];
				if(l && !/^\s*$/.test(l)) {
					l = l.split('|');
					for(j = 0; j < l.length; ++j) {
						r[j] = l[j].split('_');
					}
				}
				me.draw_frame(r);
				// load data
				if(i + 16 > me.data_index * 16 &&
					me.data_index < me.data_count) {
					me.load_data(me.data_index + 1, 2);
				}
			}
		}, 99));
	},
	start: function() {
		var me = this;
		if(me.data) {
			me.play();
		} else {
			me.load_data(0, 1, function() {
				me.play();
			});
		}
	},
	stop: function() {
		this.is_running = false;
	},
	terminal: function() {
		this.stop();
		clearInterval(this.interval);
	}
};

$('#stage').click(function() {
	viewClick();
});
$(function() {
	BadApple.play();
});
var ci=parseInt(getCookie("timeout"));
timeout=(ci>0&&ci<99999&&ci!=NaN)?ci:200;//BGM延时
var desrt="//BGM延时可能需要根据设备来调整 修改下面数字后(默认200)后点击上面执行按钮\ntimeout="+timeout+"\n\n//储存Cookie\nsetCookie('timeout',timeout)\n//刷新页面\nwindow.location.href=''\n//PS:点上面“点我开始”按钮或者点下面音乐播放按钮开始";
var my = document.getElementById("mydebug");
my.innerText=desrt;
my.style.color = "#555";

function ev(m) {
	try {
		eval(m);
	} catch(err) {
		alert(err.message);
	}
}
var is_ev = false;
var ev_E = document.getElementById("ev");
ev_E.onclick = function() {
	if(is_ev && my.innerText != "") {
		ev(my.innerText);

	} else {
		is_ev = true;
		my.innerText = desrt;
	}
};
var start=document.getElementById("start");
start.onclick=function(){
	viewClick();
};


//播放or暂停
function viewClick() {
	if(BadApple.is_running) {
		BadApple.stop();
		setTimeout(player.pause(),timeout);
	} else {
		BadApple.start();
		setTimeout(player.play(),timeout);
	}
}

//var username=document.cookie.split(";")[0].split("=")[1]; //JS操作cookies方法! 
//写cookies
function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } 

//读取cookies

function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; } 

//删除cookies

function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); } //使用示例 setCookie("name","hayden"); alert(getCookie("name")); //如果需要设定自定义过期时间 //那么把上面的setCookie　函数换成下面两个函数就ok; //程序代码 function setCookie(name,value,time) { var strsec = getsec(time); var exp = new Date(); exp.setTime(exp.getTime() + strsec*1); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } function getsec(str) { alert(str); var str1=str.substring(1,str.length)*1; var str2=str.substring(0,1); if (str2=="s") { return str1*1000; } else if (str2=="h") { return str1*60*60*1000; } else if (str2=="d") { return str1*24*60*60*1000; } } //这是有设定过期时间的使用示例： //s20是代表20秒 //h是指小时，如12小时则是：h12 //d是天数，30天则：d30 setCookie("name","hayden","s20");


$(function(){
	console.log("加载完毕");
});
